//
//  RPNetWorkErrorView.m
//  ShowYork
//
//  Created by hxh on 2020/8/25.
//  Copyright © 2020 hxh. All rights reserved.
//
typedef void(^doneBlock)(void);
typedef void(^cancelBlock)(void);

#import "RPNetWorkErrorView.h"

@interface RPNetWorkErrorView ()
@property (nonatomic,strong)UIButton *refreshBtn;
@property (nonatomic,strong)UIImageView *noNetworkImgView;
@property (nonatomic,strong)UILabel *noDataLb;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,copy)cancelBlock cancel;
@property (nonatomic,strong)UIView *navView;
@property (nonatomic,strong)UIButton *backBtn;
@property (nonatomic,strong)UILabel *navTitleLb;
@property (nonatomic,copy)NSString *navTitle;
@end

@implementation RPNetWorkErrorView

- (instancetype)initWithNavTitle:(NSString *)title completeBlock:(void (^)(void))completeBlock cancelBlock:(void (^)(void))cancelBlock{
    
    if (self = [super init]) {
        self.navTitle = title;
        [self setUI];
        if (completeBlock) {
            self.done = ^{
                completeBlock();
            };
        }
        if (cancelBlock) {
            self.cancel = ^{
                cancelBlock();
            };
        }
    }
    return self;
}



- (void)setUI{
    
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(248, 248, 248, 1);
    self.refreshBtn = [[UIButton alloc]init];
    [self.refreshBtn setTitle:@"点击刷新" forState:UIControlStateNormal];
    [self.refreshBtn setBackgroundImage:[UIImage imageNamed:@"course_login_tag"] forState:UIControlStateNormal];
    
    [self.refreshBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.refreshBtn.titleLabel.font = font(14);
    [self.refreshBtn addTarget:self action:@selector(refreshMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.refreshBtn];
    
    [self.refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(44);
        make.size.mas_equalTo(CGSizeMake(90, 34));
    }];
    
    self.noDataLb = [[UILabel alloc]init];
    self.noDataLb.text = @"当前网络不佳哦～";
    self.noDataLb.textColor = C153;
    self.noDataLb.font = font(12);
    [self addSubview:self.noDataLb];
    [self.noDataLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.refreshBtn.mas_top).offset(-48);
        make.centerX.equalTo(self.refreshBtn);
    }];

    self.noNetworkImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_network_tag"]];
    [self addSubview:self.noNetworkImgView];
    [self.noNetworkImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.noDataLb.mas_top).offset(-8);
        make.centerX.equalTo(self.noDataLb);
    }];
    
    if (self.navTitle.length > 0) {
        self.navView = [[UIView alloc]init];
        self.navView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.navView];
        
        [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(GK_STATUSBAR_HEIGHT);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(44);
        }];
        
        self.navTitleLb = [[UILabel alloc]init];
        self.navTitleLb.text = self.navTitle;
        self.navTitleLb.textColor = C35;
        self.navTitleLb.font = [UIFont boldSystemFontOfSize:18];
        [self.navView addSubview:self.navTitleLb];
        
        [self.navTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.navView);
        }];
        
        self.backBtn = [[UIButton alloc]init];
        [self.backBtn setImage:[UIImage imageNamed:@"common_close_tag"] forState:UIControlStateNormal];
        [self.backBtn setImage:[UIImage imageNamed:@"common_close_tag"] forState:UIControlStateHighlighted];
        [self.backBtn addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];
        [self.navView addSubview:self.backBtn];
        [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self.navView);
            make.width.mas_equalTo(44);
        }];
    }
}
- (void)showInMainView:(UIView *)mainView{
    [mainView addSubview:self];
}

- (void)backMethod{
    [self removeFromSuperview];
    self.cancel();
}

- (void)refreshMethod{
    NSNumber *status = [[NSUserDefaults standardUserDefaults]objectForKey:@"NetworkStatus"];
    if (status.boolValue) {
        [self removeFromSuperview];
        self.done();
    }else{
        [SVProgressHUD showErrorWithStatus:@"网络好像有点问题，请检查后重试"];
    }
}

@end
